package com.os.sys.controller;

import com.os.sys.config.MyProps;
import com.os.sys.pojo.SysRole;
import com.os.sys.service.SysRoleService;
import com.os.util.Tools;
import com.os.util.page.PageRequest;
import com.os.util.page.PageResult;
import entity.Param;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/role")
@RefreshScope
@Slf4j
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "分页查询列表信息" , notes = "分页查询列表信息")
    @RequestMapping(value="/selectByPage", method=RequestMethod.GET)
    public Result selectByPage(@RequestParam(value="page",required = false) Integer page,
                               @RequestParam(value="pageSize",required = false) Integer pageSize,
                               @RequestParam(value="roletype_id",required = false) Integer roletype_id,
                               @RequestParam(value="role_name",required = false) String role_name){
        Map map = new HashMap();
        List paramList = new ArrayList();
        if(role_name!=null && role_name.length()>0) {
            paramList.add(new Param("role_name", role_name + "", "String","like"));
        }
        if(roletype_id!=null) {
            paramList.add(new Param("roletype_id", roletype_id + "", "String","=","a"));
        }
        map.put("condstr", Tools.genCondStr("", paramList));
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(page);
        pageRequest.setPageSize(pageSize);
        PageResult pageResult = sysRoleService.selectByPage(pageRequest,map);
        Result result = new Result(true, StatusCode.OK,"查询成功",pageResult.getContent());
        result.setTotalCount(pageResult.getTotalSize());
        return result;
    }

    @ApiOperation(value = "按照ID查询信息" , notes = "按照ID查询信息")
    @ApiImplicitParam(name = "role_id",value = "用户ID",required = true,dataType = "int")
    @RequestMapping(value="/selectById/{role_id}", method=RequestMethod.GET)
    public Result selectById(@PathVariable int role_id){
        return new Result(true, StatusCode.OK,"查询成功", sysRoleService.selectById(role_id));
    }
    @ApiOperation(value = "新增角色信息" , notes = "新增角色信息")
    @RequestMapping(value="/add",method = RequestMethod.POST)
    public Result insert(@RequestBody SysRole sysRole){
        sysRoleService.insert(sysRole);
        return new Result(true, StatusCode.OK,"添加成功");
    }
    @ApiOperation(value = "更新角色信息" , notes = "更新角色信息")
    @RequestMapping(value="/update",method = RequestMethod.POST)
    public Result update(@RequestBody SysRole sysRole){
        sysRoleService.update(sysRole);
        return new Result(true, StatusCode.OK,"修改成功", sysRole);
    }
    @ApiOperation(value = "删除角色信息" , notes = "删除角色信息")
    @RequestMapping(value="/{role_id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable int role_id){
        sysRoleService.deleteById(role_id);
        return new Result(true, StatusCode.OK,"删除成功");
    }

}
